home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
motif
/
drawWidget.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
10KB
|
346 lines
/*
* Copyright (c) 1995, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/****
*
* drawWidget.c
*
* A. LaGrone, 1994
*
****/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <Xm/ToggleB.h>
#include <Xm/MenuShell.h>
#include <Xm/CascadeB.h>
#include <Xm/RowColumn.h>
#include <Xm/MainW.h>
#include <Xm/XmStrDefs.h>
#include <X11/Shell.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/GLwMDrawA.h>
int double_buffer = FALSE;
Dimension window_width, window_height;
Widget Mainwin, dialog_form, TopLevel;
Widget gl_double, gl_single;
Display *dpy;
XtAppContext appContext;
GLXContext glxc_single, glxc_double;
void drawScene(void);
void
drawScene()
{
GLUquadricObj *quadObj, *quadObj2, *quadObj3;
glLoadIdentity();
glClearColor(0.,0.,0.,0.);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
quadObj = gluNewQuadric();
gluQuadricDrawStyle(quadObj, GLU_FILL);
gluQuadricOrientation(quadObj, GLU_OUTSIDE);
gluQuadricNormals(quadObj, GLU_SMOOTH);
gluSphere(quadObj,.2,30,30);
glPushMatrix();
glTranslatef(.5, .5, 0.);
glColor3f(1.0, 1.0, 0.0);
quadObj2 = gluNewQuadric();
gluQuadricDrawStyle(quadObj2, GLU_FILL);
gluQuadricOrientation(quadObj2, GLU_OUTSIDE);
gluQuadricNormals(quadObj2, GLU_SMOOTH);
gluSphere(quadObj2,.2,30,30);
glTranslatef(-1.1, -1.1, 0.);
glColor3f(1.0, 0.0, 1.0);
quadObj2 = gluNewQuadric();
gluQuadricDrawStyle(quadObj2, GLU_FILL);
gluQuadricOrientation(quadObj2, GLU_OUTSIDE);
gluQuadricNormals(quadObj2, GLU_SMOOTH);
gluSphere(quadObj2,.2,30,30);
glPopMatrix();
if (double_buffer) {
glXSwapBuffers(dpy, XtWindow(gl_double));
}
}
void quit_action(Widget w, XtPointer arg_int, XmAnyCallbackStruct *call_data)
{
exit(0);
}
void expose_button_action(Widget w, XtPointer arg_int,
XmAnyCallbackStruct *call_data)
{
printf("Received expose callback for dialog widget\n");
}
void pbutton3_action(Widget w, XtPointer arg_int,
XmAnyCallbackStruct *call_data)
{
XtUnmanageChild(gl_double);
XtManageChild(gl_single);
GLwDrawingAreaMakeCurrent(gl_single, glxc_single);
double_buffer = FALSE;
drawScene();
}
void pbutton4_action(Widget w, XtPointer arg_int,
XmAnyCallbackStruct *call_data)
{
XtUnmanageChild(gl_single);
XtManageChild(gl_double);
GLwDrawingAreaMakeCurrent(gl_double, glxc_double);
double_buffer = TRUE;
drawScene();
}
void pbutton5_action(Widget w, XtPointer arg_int,
XmAnyCallbackStruct *call_data)
{
int i;
for (i = 0; i< 100; i++) {
drawScene();
}
}
void doubleExposeCB(Widget w, XtPointer *cd, XtPointer *call)
{
GLwDrawingAreaMakeCurrent(w, glxc_double);
drawScene();
}
void singleExposeCB(Widget w, XtPointer *cd, XtPointer *call)
{
GLwDrawingAreaMakeCurrent(w, glxc_single);
drawScene();
}
void doubleInitCB(Widget w, XtPointer *cd, XtPointer *call)
{
XVisualInfo *vi;
GLwDrawingAreaCallbackStruct *draw_struct;
draw_struct = (GLwDrawingAreaCallbackStruct *) call;
XtVaGetValues(w, GLwNvisualInfo, &vi, NULL);
glxc_double = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
GLwDrawingAreaMakeCurrent(w, glxc_double);
glMatrixMode(GL_PROJECTION);
glOrtho(-1.1,1.1,-1.1,1.1,-1.1,1.1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(10, 10, 400,400);
}
void singleInitCB(Widget w, XtPointer *cd, XtPointer *call)
{
XVisualInfo *vi;
GLwDrawingAreaCallbackStruct *draw_struct;
draw_struct = (GLwDrawingAreaCallbackStruct *) call;
XtVaGetValues(w, GLwNvisualInfo, &vi, NULL);
glxc_single = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
GLwDrawingAreaMakeCurrent(w, glxc_single);
glMatrixMode(GL_PROJECTION);
glOrtho(-1.1,1.1,-1.1,1.1,-1.1,1.1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, 400,400);
}
void create_opengl_window(Widget parent)
{
void doubleExposeCB(), singleExposeCB();
void doubleInitCB(), singleInitCB();
gl_double = XtVaCreateWidget("doublebuf", glwMDrawingAreaWidgetClass,
parent,
GLwNrgba, TRUE,
GLwNdoublebuffer, TRUE,
NULL); /* double buffered widget */
XtAddCallback(gl_double, GLwNexposeCallback, doubleExposeCB, NULL);
XtAddCallback(gl_double, GLwNginitCallback, doubleInitCB, NULL);
XtManageChild(gl_double);
gl_single = XtVaCreateWidget("singlebuf", glwMDrawingAreaWidgetClass,
parent,
GLwNrgba, TRUE,
GLwNdoublebuffer, FALSE,
NULL); /* single buffered widget */
XtAddCallback(gl_single, GLwNexposeCallback, singleExposeCB, NULL);
XtAddCallback(gl_single, GLwNginitCallback, singleInitCB, NULL);
/* map single buffered widget */
XtUnmanageChild(gl_double);
XtManageChild(gl_single);
GLwDrawingAreaMakeCurrent(gl_single, glxc_single);
drawScene();
}
main(argc, argv)
int argc;
char *argv[];
{
Arg args[10];
int argcnt=0;
int screen;
Widget button, pulldown, menubar, quit;
Widget pulldownshell, pbutton3, pbutton4, pbutton5;
Window root;
XEvent queue_event;
void quit_action();
void pbutton3_action();
void pbutton4_action();
void pbutton5_action();
XtToolkitInitialize();
appContext = XtCreateApplicationContext();
dpy = XtOpenDisplay(appContext, NULL, NULL, "Test",
NULL ,0, &argc, (String *)argv);
/* get the screen id 'screen */
screen = DefaultScreen(dpy);
/* get the root window 'root' */
root = RootWindow(dpy, screen);
TopLevel = XtAppCreateShell(NULL, "Test", applicationShellWidgetClass,
dpy, args, 0);
/* Create the mainwindow */
Mainwin = XtVaCreateManagedWidget( "x1", xmMainWindowWidgetClass,
TopLevel, XmNwidth, 400, XmNheight, 400,
XmNresizePolicy, XmRESIZE_ANY,
XmNmainWindowMarginHeight, 0,
XmNmainWindowMarginWidth, 0,
XmNborderWidth, 0,
NULL);
/* Create the menubar */
menubar = XtVaCreateManagedWidget( "menubar", xmRowColumnWidgetClass,
Mainwin,
XmNrowColumnType, XmMENU_BAR,
XmNwidth, 301, XmNheight, 60,
NULL);
/* Create the opengl window as a child of the main window */
create_opengl_window(Mainwin);
XtVaSetValues(gl_double,
XmNx, 0,
XmNy, 40,
XmNwidth, 400,
XmNheight, 400,
NULL);
XtVaSetValues(gl_single,
XmNx, 0,
XmNy, 40,
XmNwidth, 400,
XmNheight, 400,
NULL);
/* Create pulldowns for the menu bar */
pulldownshell = XtVaCreateWidget("pulldownshell",
xmMenuShellWidgetClass, Mainwin,
XmNwidth, 1,
XmNheight, 1,
XmNallowShellResize, TRUE,
XmNoverrideRedirect, TRUE,
NULL );
pulldown = XtVaCreateWidget( "m1", xmRowColumnWidgetClass,
pulldownshell,
XmNrowColumnType, XmMENU_PULLDOWN,
XmNheight, 60, NULL);
button = XtVaCreateManagedWidget( "OPTIONS",
xmCascadeButtonWidgetClass,
menubar,
XmNsubMenuId, pulldown,
NULL);
quit = XtVaCreateManagedWidget( "Quit", xmCascadeButtonWidgetClass,
menubar,
XmNlabelString,
(XmString *) XmStringCreate("QUIT", XmFONTLIST_DEFAULT_TAG),
NULL);
XtAddCallback(quit, XmNactivateCallback, quit_action, NULL);
pbutton3 = XtVaCreateManagedWidget("selection 3",
xmPushButtonWidgetClass, pulldown,
XmNlabelString,
(XmString *) XmStringCreate("Switch to single buffered",
XmFONTLIST_DEFAULT_TAG),
NULL);
XtAddCallback(pbutton3, XmNactivateCallback, pbutton3_action, NULL);
pbutton4 = XtVaCreateManagedWidget("selection 4",
xmPushButtonWidgetClass, pulldown,
XmNlabelString,
(XmString *) XmStringCreate("Switch to double buffered",
XmFONTLIST_DEFAULT_TAG),
NULL);
XtAddCallback(pbutton4, XmNactivateCallback, pbutton4_action, NULL);
pbutton5 = XtVaCreateManagedWidget("selection 5",
xmPushButtonWidgetClass, pulldown,
XmNlabelString,
(XmString *) XmStringCreate("Redraw 100 times",
XmFONTLIST_DEFAULT_TAG),
NULL);
XtAddCallback(pbutton5, XmNactivateCallback, pbutton5_action, NULL);
XtRealizeWidget(TopLevel);
while(True) {
XtAppNextEvent(appContext, &queue_event);
XtDispatchEvent(&queue_event);
}
}